package com.airlogger.log.control 
{
	import com.bourre.commands.AbstractCommand;
	
	import flash.events.Event;
	
	import com.airlogger.log.model.UIModel;
	import com.airlogger.log.model.AirLoggerModelList;
	import com.airlogger.event.SWFEvent;
	import com.airlogger.log.AirLoggerLogPlugin;
	import com.airlogger.log.model.SWFData;
	import com.bourre.log.PixlibDebug;

	/**
	 * @author ismael_perez
	 */
	public class ChangeTabName extends AbstractCommand 
	{
		override public function execute ( e : Event = null ) : void 
		{
			var evt : SWFEvent = e as SWFEvent;
			var uimodel : UIModel = getOwner().getModelLocator().getModel( AirLoggerModelList.UI ) as UIModel;
			var n : String;
			var id : String = evt.id;
			var name : String = evt.name;
			if( uimodel.isRegistered( id ) )
			{
				if ( uimodel.getRegistredName( id ).indexOf( name ) == 0 )
				{
					uimodel.getSWF( id ).name = uimodel.getRegistredName( id );
				}
				else
				{
					if( uimodel.isNameRegistered( name ) )
					{
						n = uimodel.getSWF( id ).name = name + uimodel.getNameIncrement( name );
						uimodel.registerNameWithId( id, n );
					}
					else
					{
						uimodel.registerName(name);
						uimodel.registerNameWithId(id, name);
						uimodel.getSWF( id ).name = name;
					}
				}
			}
			else
			{
				PixlibDebug.WARN( "Normalement on doit jamais passer là" );
			}
			
			uimodel.fireOnTabChanged();
		}
	}
}
